package com.shishuo.cms.service;

import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shishuo.cms.constant.SystemConstant;
import com.shishuo.cms.dao.UserDao;
import com.shishuo.cms.entity.User;
import com.shishuo.cms.entity.vo.PageVo;
import com.shishuo.cms.exception.AuthException;
import com.shishuo.cms.util.AuthUtils;

/**
 * 管理员
 * 
 * @author Useristrator
 * 
 */
@Service
public class UserService {

	@Autowired
	private UserDao userDao;

	// ///////////////////////////////
	// ///// 增加 ////////
	// ///////////////////////////////

	/**
	 * 添加管理员
	 * 
	 * @param email
	 * @param name
	 * @param password
	 * @return User
	 */
	public User addUser(String name, String password) throws AuthException {
		Date now = new Date();
		User User = new User();
		User.setName(name);
		User.setPassword(AuthUtils.getPassword(password));
		User.setCreateTime(now);
		userDao.addUser(User);
		return User;
	}

	/**
	 * 删除管理员
	 * 
	 * @param UserId
	 * @return Integer
	 */
	public int deleteUser(long UserId) {
		return userDao.deleteUser(UserId);
	}

	/**
	 * 修改管理员资料
	 *
	 * @param UserId
	 * @param name
	 * @param password
	 * @param status
	 * @return User
	 * @throws AuthException
	 */

	public void updateUserByAmdinId(long UserId, String password)
			throws AuthException {
		String pwd = AuthUtils.getPassword(password);
		userDao.updateuserByuserId(UserId, pwd);
	}

	/**
	 * 管理员登陆
	 * 
	 * @param email
	 * @param password
	 * @param request
	 * @throws IOException
	 */
	public void UserLogin(String name, String password,
			HttpServletRequest request) throws AuthException, IOException {
		User User = userDao.selectUser(name,  AuthUtils.getPassword(password));
		if (User == null) {
			throw new AuthException("邮箱或密码错误");
		}else{
			HttpSession session = request.getSession();
			User.setPassword("");
			session.setAttribute(SystemConstant.SESSION_USER, User);
		} 
	}

	/**
	 * 通过Id获得指定管理员资料
	 */
	public User getUserById(long UserId) {
		return userDao.getUserById(UserId);
	}

	/**
	 * 获得所有管理员的分页数据
	 * 
	 * @param offset
	 * @param rows
	 * @return List<User>
	 */
	public List<User> getAllList(long offset, long rows) {
		return userDao.getAllList(offset, rows);
	}

	/**
	 * 获得所有管理员的数量
	 * 
	 * @return Integer
	 */
	public int getAllListCount() {
		return userDao.getAllListCount();
	}

	/**
	 * 获得所有管理员的分页
	 * 
	 * @param Integer
	 * @return PageVo<User>
	 */
	public PageVo<User> getAllListPage(int pageNum) {
		PageVo<User> pageVo = new PageVo<User>(pageNum);
		pageVo.setRows(20);
		List<User> list = this
				.getAllList(pageVo.getOffset(), pageVo.getRows());
		pageVo.setList(list);
		pageVo.setCount(this.getAllListCount());
		return pageVo;
	}

	public int getUserByName(String name) {
		return userDao.getuserCountByUserName(name);
	}
}
